/**
 * @(#)SerializadorXstream.java 1/04/2010
 *
 * Copyright (c) 2005-2010 PsiqueWare S.A. de C.V. All Rights Reserved.
 * This software is the proprietary information PsiqueWare S.A. de C.V.
 * Use is subject to license terms.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * <br/>Usuario : AngelElectrico
 * <br/>Creaci&oacute;n: 1/04/2010 - 10:54:32 AM
 * <br/>
 * <br/>
 * <br/><t5>Cambios :</t5>
 * <br/><b>Fecha | Autor  |  Cambios</b>
 * <ul>
 * <li>1/04/2010 | <a href = "mailto:AngelElectrico@PsiqueWare.com">&Aacute;ngel El&eacute;ctrico</a> | Creaci&oacute;n del Objeto.</li>
 *
 * </ul>
 */

package com.psiqueware.alheli.serializacion.serializadores;

import com.psiqueware.alheli.dao.hibernate.HibernatePersistenceDelegate;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.converters.collections.MapConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.Mapper;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * {@link Serializador} basado en {@link XStream}.
 * <br/>Paquete Original: com.psiqueware.alheli.serializacion.xstream
 * <br/>Nombre  Original: SerializadorXstream
 * <br/>Oprima para ver : <a href="SerializadorXstream.java.html"><i>C&oacute;digo Fuente</i> </a>
 *
 * @author <a href="mailto:AngelElectrico@PsiqueWare.com">AngelElectrico</a>
 * @version Revision: 1.1 1/04/2010 10:54:32 AM
 */
public final class SerializadorXStreamHibernate extends SerializadorXStream
{

    /**
     * Devuelve una instance de {@link XStream} inicializada correctamente.
     *
     * @return instancia de {@link XStream}.
     */
    protected XStream getInstance()
    {
        final XStream xstream = new XStream(new DomDriver());

        final Map<Class, Class> delegates = HibernatePersistenceDelegate.getSecondaryDelegatedClasses();
        for (final Object key : delegates.keySet())
        {
            final Class k = (Class) key;
            final Class value = delegates.get(k);
            xstream.addDefaultImplementation(k, value);
        }

        Mapper mapper = xstream.getMapper();
        xstream.registerConverter(new HibernateCollectionConverter(mapper));
        xstream.registerConverter(new HibernateMapConverter(mapper));
        return xstream;
    }

    /**
     * {@link CollectionConverter} para hibernate.
     * <br/>Paquete Original: com.psiqueware.alheli.serializacion.xstream
     * <br/>Nombre  Original: SerializadorXStream
     * <br/><a href="SerializadorXStream.java.html"><i>Ver C&oacute;digo Fuente</i> </a>
     *
     * @author <a href = "mailto:AngelElectrico@PsiqueWare.com">&Aacute;ngel El&eacute;ctrico</a>
     * @version Revision: 1.0
     */
    private static class HibernateCollectionConverter extends CollectionConverter
    {

        HibernateCollectionConverter(Mapper mapper)
        {
            super(mapper);
        }

        public boolean canConvert(Class type)
        {
            return super.canConvert(type) || type == List.class || type == Set.class;
        }
    }

    /**
     * {@link MapConverter} para hibernate.
     * <br/>Paquete Original: com.psiqueware.alheli.serializacion.xstream
     * <br/>Nombre  Original: SerializadorXStream
     * <br/><a href="SerializadorXStream.java.html"><i>Ver C&oacute;digo Fuente</i> </a>
     *
     * @author <a href = "mailto:AngelElectrico@PsiqueWare.com">&Aacute;ngel El&eacute;ctrico</a>
     * @version Revision: 1.0
     */
    private static class HibernateMapConverter extends MapConverter
    {

        HibernateMapConverter(Mapper mapper)
        {
            super(mapper);
        }

        public boolean canConvert(Class type)
        {
            return super.canConvert(type) || type == Map.class;
        }
    }
}